package com.fqgj.sentry.controller.pc;

import com.fqgj.sentry.biz.pc.RuleBusiness;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.controller.pc.request.rule.*;
import com.fqgj.sentry.policy.engine.domain.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2017/7/19
 * Time: 下午5:15
 */
@RequestMapping("/rule")
@RestController
public class RuleController {
    @Autowired
    private RuleBusiness ruleBusiness;


    /**
     * 规则查询
     *
     * @return
     */
    @RequestMapping(value = "/{policyId}/list", method = RequestMethod.POST)
    public ApiOutput list(@PathVariable(value = "policyId") Long policyId) {
        return new ApiOutput<>("list", ruleBusiness.findRuleList(policyId));
    }


    /**
     * 规则 集合排序
     *
     * @return
     */
    @RequestMapping(value = "/sort/update", method = RequestMethod.POST)
    public ApiOutput updatePolicySort(@RequestBody RuleSortVo ruleSortVo) {
        ruleBusiness.updateRuleSort(ruleSortVo);
        return new ApiOutput<>();
    }


    /**
     * 规则查询
     *
     * @return
     */
    @RequestMapping(value = "/{policyId}/common", method = RequestMethod.POST)
    public ApiOutput commonRule(@PathVariable(value = "policyId") Long policyId) {
        return new ApiOutput<>("commonRule", ruleBusiness.findCommonRuleByPolicy(policyId));
    }


    /**
     * 查询 规则条件
     *
     * @return
     */
    @RequestMapping(value = "/{ruleId}/condition", method = RequestMethod.POST)
    public ApiOutput ruleCondition(@PathVariable(value = "ruleId") Long ruleId) {
        Rule rule = ruleBusiness.ruleCondition(ruleId);
        return new ApiOutput<>("rule", rule);
    }

    /**
     * 规则删除
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiOutput delete(@RequestBody RuleOperateVo ruleIdsVo) {
        ruleBusiness.ruleDelete(ruleIdsVo.getRuleIds());
        return new ApiOutput<>();
    }


    /**
     * 规则  开关更新
     *
     * @return
     */
    @RequestMapping(value = "/active/update", method = RequestMethod.POST)
    public ApiOutput updateRuleActive(@RequestBody RuleOperateVo ruleIdsVo) {
        ruleBusiness.updateRuleActive(ruleIdsVo.getRuleIds(), ruleIdsVo.getActiveType());
        return new ApiOutput<>();
    }

    /**
     * 根据  code查询规则
     *
     * @return
     */
    @RequestMapping(value = "/history/list", method = RequestMethod.POST)
    public ApiOutput findByCondition(@RequestBody RuleCodeVo ruleCode) {
        return new ApiOutput<>("list", ruleBusiness.findByCondition(ruleCode.getCode()));
    }


    /**
     * 规则新增
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiOutput addRule(@RequestBody RuleAddVo ruleAddVo) {
        ruleBusiness.addRule(ruleAddVo);
        return new ApiOutput<>();
    }


    /**
     * 规则修改
     *
     * @return
     */
    @RequestMapping(value = "/{ruleId}/update", method = RequestMethod.POST)
    public ApiOutput updateRule(@PathVariable(value = "ruleId") Long ruleId, @RequestBody RuleUpdateVo ruleUpdateVo) {
        ruleBusiness.updateRule(ruleId, ruleUpdateVo);
        return new ApiOutput<>();
    }


}
